class Order < ActiveRecord::Base
  
  belongs_to :user
  has_many :order_items, :include => :product, :dependent => :delete_all, :autosave => true

  @@payment_types = {
    :creditcard => 1,
    :account    => 2
  }
  
  def self.from_checkout(checkout, user_id)

    order = Order.new

    order.payment_type  = checkout.payment_type
    order.user_id       = user_id
    order.shipping_cost = checkout.shipping_cost
    order.total_cost    = checkout.total_cost
    order.status        = checkout.paid? ? 2 : 1

    [
      'street',
      'house_number',
      'house_number_addition',
      'city',
      'postal_code',
      'telephone_number',
      'mobile_number',
      'fax_number',
      'company'
    ].each do |i|
      order.write_attribute('address_' + i, checkout.address.read_attribute(i))
    end

    [
      'first_name',
      'surname',
      'email'
    ].each do |i|
      order.write_attribute('contact_' + i, checkout.contact.read_attribute(i))
    end

    checkout.cart.cart_items.each do |item|
      order.order_items.build(
          :product_id       => item.product.id,
          :original_price   => item.product.price,
          :quantity         => item.quantity,
          :shipped_quantity => 0
      )
      
    end

    return order
  end

  def user?
    user != nil
  end

  def total_products
    total = 0

    order_items.each do |item|
      total += item.quantity
    end

    return total
 end
 
 #Als de status 3 of 4 is (dus verzonden) dan worden alle shipped_quantity in orderitems op de totaal besteld gezet en de voorraad van het product bijgesteld
 #verder als de status 4 is, worden alle persoonlijke gegevens binnen de order verwijdert. De koppeling met de user blijft bestaan, maar Gasten blijven zo anoniem
 def update_order()
   if(status == 4 || status == 3)
    order_items.each do |item|
      shipped = item.change_shipped()
    end
   elsif(status == 5)
    order_items.each do |item|
      item.shipped_quantity = 0
      item.save
    end
  end
  if(status == 4)
    self.contact_email = ""
    self.contact_surname = ""
    self.contact_first_name = ""
    self.address_city = ""
    self.address_street = ""
    self.address_house_number = ""
    self.address_house_number_addition = ""
    self.address_postal_code = ""
    self.address_telephone_number = ""
    self.address_fax_number = ""
    self.address_company = ""
    self.address_mobile_number = ""
    self.save
  end
 end
 
 #controlleert of alle order_items compleet verzonden zijn
 def check_status_by_items()
   check = true
   order_items.each do |item|
     check = check && item.check_fully_shipped()
   end
    return check
 end
 
 #geeft terug welke status er moet worden gegeven als de producten verzonden zijn.
 
 def update_status_to_shipped()
   if(status == 1) #niet betaald, niet verzonden
    return 3 #niet betaald, verzonden
   elsif(status == 2) #betaald, niet verzonden
    return 4 #betaald, verzonden
  end
 end
end
